/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.assembler;

import com.inspur.edp.bff.api.manager.IFSManagerContext;
import com.inspur.edp.bff.api.manager.assembler.IExtendDeleteAssembler;
import com.inspur.edp.bff.api.manager.context.DeleteContext;
import com.inspur.edp.bff.spi.action.delete.AbstractDeleteAction;
import com.inspur.edp.bff.spi.action.delete.AfterDeleteAction;
import com.inspur.edp.bff.spi.action.delete.BeforeDeleteAction;
import com.inspur.edp.bff.spi.action.delete.DefaultDeleteAction;
import com.inspur.edp.bff.spi.action.multidelete.AbstractMultiDeleteAction;
import com.inspur.edp.bff.spi.action.multidelete.AfterMultiDeleteAction;
import com.inspur.edp.bff.spi.action.multidelete.BeforeMultiDeleteAction;
import com.inspur.edp.bff.spi.action.multidelete.DefaultMultiDeleteAction;

import java.util.ArrayList;

public abstract class AbstractExtendDeleteAssembler implements IExtendDeleteAssembler {


    protected abstract ArrayList<BeforeDeleteAction> getBeforeDeleteActions(DeleteContext delContext);

    protected abstract ArrayList<AbstractDeleteAction> getDeleteActions(DeleteContext delContext);

    protected abstract ArrayList<AfterDeleteAction> getAfterDeleteActions(DeleteContext delContext);

    protected ArrayList<AbstractMultiDeleteAction> getMultiDeleteActions(DeleteContext deleteContext) {
        return new ArrayList<AbstractMultiDeleteAction>();
    }


    protected ArrayList<BeforeMultiDeleteAction> getBeforeMultiDeleteActions(DeleteContext deleteContext) {
        return new ArrayList<BeforeMultiDeleteAction>();
    }


    protected ArrayList<AfterMultiDeleteAction> getAfterMultiDeleteActions(DeleteContext deleteContext) {
        return new ArrayList<AfterMultiDeleteAction>();
    }


    public final void doBeforeMultiDelete(IFSManagerContext context, DeleteContext delContext) {
        ArrayList<BeforeMultiDeleteAction> actions = getBeforeMultiDeleteActions(delContext);
        if (actions == null || actions.size() < 1) {
            return;
        }


        for (BeforeMultiDeleteAction action : actions) {
            action.deal(context);
        }
    }


    public final void doMultiDelete(IFSManagerContext context, DeleteContext delContext) {
        ArrayList<AbstractMultiDeleteAction> actions = getMultiDeleteActions(delContext);
        if (actions == null || actions.size() < 1) {

            actions = new ArrayList<AbstractMultiDeleteAction>();
            DefaultMultiDeleteAction detaultAction = new DefaultMultiDeleteAction(delContext);
            actions.add(detaultAction);
        }

        for (AbstractMultiDeleteAction action : actions) {
            action.deal(context);
        }
    }


    public final void doAfterMultiDelete(IFSManagerContext context, DeleteContext delContext) {
        ArrayList<AfterMultiDeleteAction> actions = getAfterMultiDeleteActions(delContext);
        if (actions == null || actions.size() < 1) {
            return;
        }

        for (AfterMultiDeleteAction action : actions) {
            action.deal(context);
        }
    }

    public final void doBeforeDelete(IFSManagerContext context, DeleteContext delContext) {
        ArrayList<BeforeDeleteAction> actions = getBeforeDeleteActions(delContext);
        if (actions == null || actions.size() < 1) {
            return;
        }

        for (BeforeDeleteAction action : actions) {
            action.deal(context);
        }
    }

    public final void doDelete(IFSManagerContext context, DeleteContext delContext) {
        ArrayList<AbstractDeleteAction> actions = getDeleteActions(delContext);
        if (actions == null || actions.size() < 1) {
            actions = new ArrayList<AbstractDeleteAction>();
            DefaultDeleteAction detaultAction = new DefaultDeleteAction(delContext);
            actions.add(detaultAction);
        }

        for (AbstractDeleteAction action : actions) {
            action.deal(context);
        }
    }

    public final void doAfterDelete(IFSManagerContext context, DeleteContext delContext) {
        ArrayList<AfterDeleteAction> actions = getAfterDeleteActions(delContext);
        if (actions == null || actions.size() < 1) {
            return;
        }
        for (AfterDeleteAction action : actions) {
            action.deal(context);
        }
    }
}
